package com.baizhi.controller;

import com.baizhi.entity.Singer;
import com.baizhi.service.SingerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("singer")
public class SingerController {
    @Autowired
    private SingerService singerService;
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Singer> selectAll(){
        List<Singer> singers = singerService.selectAll();
        return singers;
    }
    @RequestMapping("selectPage")
    @ResponseBody
    public Map selectPage(int page, int limit, String singerName){
        PageInfo<Singer> singerPageInfo = singerService.selectPage(page,limit,singerName);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",singerPageInfo.getTotal());
        map.put("data",singerPageInfo.getList());
        return map;
    }

    @RequestMapping("insert")
    @ResponseBody
    public Map insert(Singer singer){

        Map map = new HashMap();
        try{
            singerService.insert(singer);
            map.put("isInsert",true);
        }catch (Exception e){
            map.put("isInsert",false);
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("delete")
    public Map delete(Integer id){
        Map map = new HashMap();
        try{
            singerService.delete(id);
            map.put("isDelete",true);
        }catch (Exception e){
            map.put("isDelete",false);
        }
        return map;
    }
    @RequestMapping("deleteMany")
    @ResponseBody
    public Map delete(Integer[] ids){
        Map map = new HashMap();
        try{
            singerService.deleteMany(ids);
            map.put("isDelete",true);
        }catch (Exception e){
            map.put("isDelete",false);
        }
        return map;
    }
    @RequestMapping("selectOne")
    @ResponseBody
    public Singer selectOne(Integer id){
        Singer singer = singerService.selectOne(id);
        return singer;
    }
    @RequestMapping("update")
    @ResponseBody
    public Map delete(Singer singer){

        Map map = new HashMap();
        try{
            singerService.update(singer);
            map.put("isUpdate",true);
        }catch (Exception e){
            map.put("isUpdate",false);
        }
        System.out.println("map = " + map);
        return map;
    }
}
